//
//  CustomStatisticsTableViewController.swift
//  ZNZXSuperMarket
//
//  Created by 四川中农 on 2018/8/7.
//  Copyright © 2018年 四川中农. All rights reserved.
//

import UIKit

class CustomStatisticsTableViewController: BaseTableViewController {
    
    @IBOutlet weak var startLabel: UILabel!
    @IBOutlet weak var endLabel: UILabel!
    lazy var effect:EffectView = {
        let view = EffectView.init(frame: CGRect.init(x: 0, y: 0, width: UIScreen.ScreenWidth, height: UIScreen.ScreenHeight))
        view.alpha = 0
        UIApplication.shared.keyWindow?.addSubview(view)
        view.hidenClosure = { [weak self]  in
            self?.pickerViewHiden()
        }
        return view
    }()
    
    var pickerView : SeleDatePicker?
    
    var startDate:Date?
    var endDate:Date?
    
    var isStart = false

    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem
        self.title = "自定义统计"
        self.navigationController?.setNavigationBarHidden(false, animated: true)
    }
    
    override func backBarButtonMethon() {
        self.pickerView?.removeFromSuperview()
        for ctrl in (self.navigationController?.viewControllers)! {
            if ctrl is EarningsStatementTableViewController {
                self.navigationController?.popToViewController(ctrl, animated: true)
            }
        }
    }
    
    
    @IBAction func startStatistics(_ sender: UIButton) {
        
        if self.startDate == nil {
            PKhud.showText(text: "请选择开始时间~")
            return
        }
        if self.endDate == nil {
            PKhud.showText(text: "请选择结束时间~")
            return
        }
        
        let result:ComparisonResult = startDate!.compare(endDate!)
        if result == .orderedDescending {
            PKhud.showText(text: "结束时间不能小于开始时间~")
            return
        }
        
        let ctrl = Utils.instantiateInitialViewControllerWith(storyBoardName: "Main", identitier: "CustomStatisticsListTableViewController") as! CustomStatisticsListTableViewController
        ctrl.startTime = (self.startDate?.dateNowAsString("YYYY-MM-dd"))!
        ctrl.endTime = (self.endDate?.dateNowAsString("YYYY-MM-dd"))!
        self.navigationController?.pushViewController(ctrl, animated: true)
        
    }
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 2
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row == 0 {
            self.isStart = true
        }else{
            self.isStart = false
        }
        self.showDatePicker()
    }
    
    // MARK:显示时间选择器
    func showDatePicker(){
        
        if self.pickerView == nil {
            self.effect.alpha = 0
            pickerView = SeleDatePicker(frame:CGRect(x: 0, y: UIScreen.ScreenHeight, width: UIScreen.ScreenWidth, height: 220 + (Utils.isIphoneX() ? 34 : 0)))
            pickerView!.datePicker.datePickerMode = .date
            pickerView!.backClosure = { [weak self] () in
                self?.pickerViewHiden()
            }
            pickerView!.backDate = { [weak self] (value) in
                self?.pickerViewHiden()
                if (self?.isStart)! {
                    self?.startDate = value
                    self?.startLabel.text = self?.startDate?.dateNowAsString("YYYY-MM-dd")
                }else{
                    self?.endDate = value
                    self?.endLabel.text = self?.endDate?.dateNowAsString("YYYY-MM-dd")
                }
            }
            UIApplication.shared.keyWindow?.addSubview(pickerView!)
        }
        
        if self.isStart && self.startDate != nil    {
            self.pickerView!.date = self.startDate
        }
        
        if self.isStart == false && self.endDate != nil    {
            self.pickerView!.date = self.endDate
        }
        
        UIView.animate(withDuration: 0.1, animations: {
            self.effect.alpha = 1
            
        }) { (_) in
            UIView.animate(withDuration: 0.3, animations: {
                self.pickerView!.top = UIScreen.ScreenHeight - 220 - (Utils.isIphoneX() ? 34 : 0)
            })
        }
    }
    
    // MARK: 隐藏时间选择器
    func pickerViewHiden(){
        UIView.animate(withDuration: 0.2, animations: {
            self.pickerView!.top = UIScreen.ScreenHeight
        }) { (_) in
            self.effect.alpha = 0
        }
    }

    /*
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)

        // Configure the cell...

        return cell
    }
    */

    /*
    // Override to support conditional editing of the table view.
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    */

    /*
    // Override to support editing the table view.
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            // Delete the row from the data source
            tableView.deleteRows(at: [indexPath], with: .fade)
        } else if editingStyle == .insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    */

    /*
    // Override to support rearranging the table view.
    override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {

    }
    */

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the item to be re-orderable.
        return true
    }
    */

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
